package com.sixmac.util;

import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.toolbox.kit.RandomKit;
import com.sixmac.module.*;
import org.apache.commons.lang.math.RandomUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class RandomValue {
    public static String base = "abcdefghijklmnopqrstuvwxyz0123456789";
    private static String firstName="赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮卞齐康伍余元卜顾孟平黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董梁杜阮蓝闵席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田樊胡凌霍虞万支柯咎管卢莫经房裘缪干解应宗宣丁贲邓郁单杭洪包诸左石崔吉钮龚程嵇邢滑裴陆荣翁荀羊於惠甄魏加封芮羿储靳汲邴糜松井段富巫乌焦巴弓牧隗山谷车侯宓蓬全郗班仰秋仲伊宫宁仇栾暴甘钭厉戎祖武符刘姜詹束龙叶幸司韶郜黎蓟薄印宿白怀蒲台从鄂索咸籍赖卓蔺屠蒙池乔阴郁胥能苍双闻莘党翟谭贡劳逄姬申扶堵冉宰郦雍却璩桑桂濮牛寿通边扈燕冀郏浦尚农温别庄晏柴瞿阎充慕连茹习宦艾鱼容向古易慎戈廖庚终暨居衡步都耿满弘匡国文寇广禄阙东殴殳沃利蔚越夔隆师巩厍聂晁勾敖融冷訾辛阚那简饶空曾毋沙乜养鞠须丰巢关蒯相查后江红游竺权逯盖益桓公万俟司马上官欧阳夏侯诸葛闻人东方赫连皇甫尉迟公羊澹台公冶宗政濮阳淳于仲孙太叔申屠公孙乐正轩辕令狐钟离闾丘长孙慕容鲜于宇文司徒司空亓官司寇仉督子车颛孙端木巫马公西漆雕乐正壤驷公良拓拔夹谷宰父谷粱晋楚阎法汝鄢涂钦段干百里东郭南门呼延归海羊舌微生岳帅缑亢况后有琴梁丘左丘东门西门商牟佘佴伯赏南宫墨哈谯笪年爱阳佟第五言福百家姓续";
    private static String girl="秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽 ";
    private static String boy="伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘";
    private static String citys = "临沂市";
    private static String[] road="市区".split(",");
    private static final String[] email_suffix="@gmail.com,@yahoo.com,@msn.com,@hotmail.com,@aol.com,@ask.com,@live.com,@qq.com,@0355.net,@163.com,@163.net,@263.net,@3721.net,@yeah.net,@googlemail.com,@126.com,@sina.com,@sohu.com,@yahoo.com.cn".split(",");

    public static int getNum(int start,int end) {
        return (int)(Math.random()*(end-start+1)+start);
    }

    /**
     * 返回Email
     * @param lMin 最小长度
     * @param lMax 最大长度
     * @return
     */
    public static String getEmail(int lMin,int lMax) {
        int length=getNum(lMin,lMax);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = (int)(Math.random()*base.length());
            sb.append(base.charAt(number));
        }
        sb.append(email_suffix[(int)(Math.random()*email_suffix.length)]);
        return sb.toString();
    }

    public static String getBase(int lMin,int lMax) {
        int length=getNum(lMin,lMax);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = (int)(Math.random()*base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 返回手机号码
     */
    private static String[] telFirst="134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");
    private static String getTel() {
        int index=getNum(0,telFirst.length-1);
        String first=telFirst[index];
        String second=String.valueOf(getNum(1,888)+10000).substring(1);
        String third=String.valueOf(getNum(1,9100)+10000).substring(1);
        return first+second+third;
    }

    /**
     * 返回中文姓名
     */
    private static String name_sex = "";
    private static Integer id_sex = 1;
    private static String getChineseName() {
        int index=getNum(0, firstName.length()-1);
        String first=firstName.substring(index, index+1);
        int sex=getNum(0,1);
        String str=boy;
        int length=boy.length();
        if(sex==0){
            str=girl;
            length=girl.length();
            name_sex = "女";
            id_sex = 2;
        }else {
            name_sex="男";
            id_sex = 1;
        }
        index=getNum(0,length-1);
        String second=str.substring(index, index+1);
        int hasThird=getNum(0,1);
        String third="";
        if(hasThird==1){
            index=getNum(0,length-1);
            third=str.substring(index, index+1);
        }
        return first+second+third;
    }




    /**
     * 返回地址
     * @return
     */
    private static String getRoad() {
        int index=getNum(0,road.length-1);
        String first="山东省"+citys+road[index];
        String second=String.valueOf(getNum(11,150))+"号";
        String third="-"+getNum(1,20)+"-"+getNum(1,10);
        return first+second+third;
    }

    /**
     * 数据封装
     * @return
     */
    public static void getAddress() {

//        String name = getChineseName();
//        Landlord landlord = new Landlord();
//        landlord.setMobile(getTel());
//        landlord.setCreateDate(randomDate("2018-01-01 00:00:00","2018-11-06 00:00:00"));
//        landlord.setBirthday(randomDate("1980-01-01 00:00:00","2000-01-01 00:00:00"));
//        landlord.setIdCard("412123199405210000");
//        landlord.setName(name);
//        landlord.setUpdateDate(new Date());
//        landlord.setSex(id_sex);
//        landlord.setRegion(371301);
//
//        int id = Blade.create(Landlord.class).saveRtId(landlord);
//
//        Room room = new Room();
//        room.setLandlordId(82);
//        room.setRoomNo(name+ RandomKit.randomInt(1,3)+"号");
//        room.setAddress(getRoad());
//        room.setLat(getLan());
//        room.setLng(getlng());
//        room.setLandlordId(id);
//        room.setUpdateDate(new Date());
//        room.setRegion(371301);
//        room.setLockNo(getBase(16,26));
//        room.setCreateDate(randomDate("2018-01-01 00:00:00","2018-11-06 00:00:00"));
//
//        Blade.create(Room.class).saveRtId(room);
//        Tenant tenant = new Tenant();
//        String name = getChineseName();
//        tenant.setName(name);
//        tenant.setSex(id_sex);
//        tenant.setMobile(getTel());
//        tenant.setBirthday(randomDate("1980-01-01 00:00:00","2000-01-01 00:00:00"));
//        tenant.setCreateDate(randomDate("2018-01-01 00:00:00","2018-11-06 00:00:00"));
//        Integer id = Blade.create(Tenant.class).saveRtId(tenant);

//        TenantRoom tenantRoom = new TenantRoom();
//        tenantRoom.setTenantId(id);
//        Date create = randomDate("2018-01-01 00:00:00","2018-09-06 00:00:00");
//        tenantRoom.setStartDate(create);
//        tenantRoom.setEndDate(randomDate("2018-09-07 00:00:00","2018-09-06 00:00:00"));
//        tenantRoom.setRoomId(RandomKit.randomInt(991,1987));
//        tenantRoom.setCreateDate(create);
//        tenantRoom.setApprove(1);
//        int status = RandomKit.randomInt(1,3);
//        tenantRoom.setStatus(status);
//        int trid = Blade.create(TenantRoom.class).saveRtId(tenantRoom);

//        Contract contract = new Contract();
//        contract.setName(name);
//        contract.setTenantRoomId(trid);
//        contract.setTentantId(id);
//        contract.setRoomId(tenantRoom.getRoomId());
//        contract.setStatus(status==1?0:1);
//        contract.setStartTime(tenantRoom.getStartDate());
//        contract.setEndTime(tenantRoom.getEndDate());
//        contract.setOutTime(new Date());
//        Blade.create(Contract.class).saveRtId(contract);

          List<Landlord> landlords = Blade.create(Landlord.class).findAll();
            for (Landlord landlord:landlords){
                CreateIDCardNo cre = new CreateIDCardNo();
                String randomID = cre.getRandomID();
                landlord.setIdCard(randomID);
                Blade.create(Landlord.class).update(landlord);
            };

        List<Tenant> tenants = Blade.create(Tenant.class).findAll();
        for (Tenant tenant:tenants){
            CreateIDCardNo cre = new CreateIDCardNo();
            String randomID = cre.getRandomID();
            tenant.setIdCard(randomID);
            Blade.create(Tenant.class).update(tenant);
        };

//        int trid = RandomKit.randomInt(1218,2224);
//        TenantRoom tenantRoom = Blade.create(TenantRoom.class).findById(trid);
//        int n = RandomKit.randomInt(0,3);
//        for (int i=0;i<n;i++){
//            int type =  RandomKit.randomInt(1,3);
//            OpenHistory openHistory = new OpenHistory();
//            openHistory.setType(type==1?0:1);
//            openHistory.setTenantId(tenantRoom.getTenantId());
//            openHistory.setCreateDate(randomDate("2018-011-01 00:00:00","2018-11-06 00:00:00"));
//            openHistory.setRoomId(tenantRoom.getRoomId());
//            openHistory.setUpdateDate(new Date());
//            Blade.create(OpenHistory.class).saveRtId(openHistory);
//        }

//        Map map=new HashMap();
//        map.put("name", getChineseName());
//        map.put("sex", name_sex);
//        map.put("road", getRoad());
//        map.put("tel", getTel());
//        map.put("email", getEmail(6,9));
//        map.put("lat", getLan());
//        map.put("lng", getlng());
    }

    public static String getLan(){
        Random random = new Random();
        int i = random.nextInt(1000);
        double address = i*0.0001;
        double lng = 35.024995;
        lng = lng+address;

        BigDecimal bg=new BigDecimal(lng+"");
        return bg.toString();
    }

    public  static  String getlng(){
        Random random = new Random();
        int j = random.nextInt(1000);
        double address2 = j*0.0001;
        double lab = 118.263933;
        lab = lab+address2;

        BigDecimal bg2=new BigDecimal(lab+"");
        return bg2.toString();
    }

    public static void main(String[] args) {
//        for (int i = 0; i < 100; i++) {
//            System.out.println(getAddress());
////          System.out.println(getEmailName(6,9));
//        }
        getAddress();
        System.out.println(randomDate("1980-01-01 00:00:00","2000-01-01 00:00:00"));
    }

    private static Date randomDate(String beginDate, String endDate) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Date start = format.parse(beginDate);// 开始日期
            Date end = format.parse(endDate);// 结束日期
            if (start.getTime() >= end.getTime()) {
                return null;
            }
            long date = random(start.getTime(), end.getTime());

            return new Date(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static long random(long begin, long end) {
        long rtnn = begin + (long) (Math.random() * (end - begin));
        if (rtnn == begin || rtnn == end) {
            return random(begin, end);
        }
        return rtnn;
    }
}
